package cn.t.modules.scheduler.core.schedule.task;

import cn.t.modules.scheduler.common.enums.ScheduleStatus;
import cn.t.modules.scheduler.core.SpringBeanContext;
import cn.t.modules.scheduler.core.SqlProcessor;
import cn.t.modules.timewheel.core.hierarchical.AbstractTask;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 陶敏麒
 * @date 2024/3/7 11:17
 */
@EqualsAndHashCode(callSuper = true)
@Slf4j
public abstract class BaseTask extends AbstractTask {

    protected SqlProcessor sqliteProcessor;
    protected BaseTask(String scheduleId, String scheduleName, String content, int version, long execTs) {
        super(scheduleId, scheduleName, content, version, execTs);
        this.sqliteProcessor = SpringBeanContext.getBean(SqlProcessor.class);
    }

    /**
     * 执行
     */
    @Override
    public void process() {
        // 这里会导致10ms左右的延时
        if (this.sqliteProcessor.hasChanged(super.taskId, super.version, ScheduleStatus.RUNNING)) {
            log.warn("当前计划[{},{}]已经变更,不执行", super.scheduleId, super.scheduleName);
            return;
        }
        this.execute();
    }
}
